<?php

namespace App\Models;

use App\Http\Controllers\Resource\BkController;
use App\Http\Controllers\Resource\ZwzxController;
use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\Cache;

/*数字资源（乐儿）model*/

class ResourceLeer extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'resource_leer';

    /*关联活动类型*/
    public function conBigType()
    {
        return $this->hasOne(ResourceType::class, 'id', 'big_type_id');
    }


    /*关联活动类型*/
    public function conType()
    {
        return $this->hasOne(ResourceTypeExternal::class, 'id', 'type_id');
    }


    /**
     * 获取书籍详情
     * @param id  书籍id
     * @param guid  书籍guid  和 书籍id 2选一
     * @param resource_business_id  资源商id
     */
    public function detail($id = null, $guid = null)
    {
        if (empty($id) && empty($guid)) {
            return false;
        }
        $res = $this->select([
            'id',
            'guid',
            'resource_business_id',
            'title',
            // 'author',
            // 'isbn',
            // 'press',
            // 'pre_time',
            'postfix', //返回调整方式
            //  'link', //乐儿资源专属
            'img',
            'intro',
            'create_time',
            'big_type_id',
            'type_id',
            'browse_num',
            'is_recom'
        ])->with('conBigType', function ($query) {
            $query->select('id', 'type_name')->where('is_del', 1);
        })->with('conType', function ($query) {
            $query->select('id', 'type_name')->where('is_del', 1);
        })->where(function ($query) use ($id, $guid) {
            if ($id) {
                $query->where('id', $id);
            } else {
                $query->where('guid', $guid);
            }
        })->where('is_del', 1)
            ->first();

        if ($res) {
            $is_exists_chapter = ResourceBusinessName::where('id', $res->resource_business_id)->value('is_exists_chapter');
            $res->is_exists_chapter = $is_exists_chapter == 1 ? true : false; //是否存在章节，有则可以获取
        }
        return $res;
    }

    /**
     * 添加数据到数据库
     * @param $data
     */
    public function addData($data)
    {
        if (empty($data['data_unique_id'])) {
            return false;
        }

        $res = $this->where('data_unique_id', $data['data_unique_id'])->first(); //获取所有数据，之前下架的现在重新上架
        if (empty($res)) {
            //修改
            $res = new self();
            $guid = get_guid();
            $res->guid = $guid;
        }
        $res->data_unique_id = $data['data_unique_id'];
        $res->resource_business_id = 5; //乐儿
        $res->big_type_id = $data['big_type_id'];
        $res->type_id = $data['type_id'];
        // $res->press = $data['press'];
        // $res->pre_time = $data['publish'];
        $res->title = $data['title'];
        $res->img = $data['img'];
        $res->intro = $data['intro'] ?? '';
        $res->link = $data['link'];

        $res->is_del = 1;
        $res->save();

        //加入到资源总数据库
        $resourceTotalModel = new ResourceTotal();
        $resourceTotalModel->addData($res);

        return $res->id;
    }

    /**
     * 获取图书阅读跳转地址
     * @param guid  章节id
     */
    public function getResourceSkipAddress($guid)
    {
        $res = $this->select('link')->where('is_del', 1)->where('guid', $guid)->first();
        if (empty($res)) {
            return false;
        }
        return $res;
    }
}
